package cn.relx.test;

import cn.relx.utils.ESClientUtil;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class EsTestTwo {
    @Test
    public void TestDel(){
        TransportClient client = ESClientUtil.getClient();
        client.prepareDelete("pethome","employee","1").get();
    }

    @Test
    public void TestAdd(){
        TransportClient client = ESClientUtil.getClient();
        for (int i = 1; i < 50; i++) {
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("pet", "employee");
            indexRequestBuilder.setId(i+"");
            Map<String,Object> map = new HashMap<String, Object>();
            if(i%3==0){
                map.put("name","漩涡鸣人");
            }else if(i%2==0){
                map.put("name","卡卡西");
            }else {
                map.put("name","宇智波佐助");
            }
            map.put("age",i);
            map.put("sex",i%2==0);
            indexRequestBuilder.setSource(map).get();
        }
    }

    @Test
    public void TestUpdate(){
        TransportClient client = ESClientUtil.getClient();
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pet", "employee", "1");
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("name","宇智波鼬");
        updateRequestBuilder.setDoc(map).get();
    }

    @Test
    public void TestGet(){
        TransportClient client = ESClientUtil.getClient();
        GetResponse documentFields = client.prepareGet("pet", "employee", "1").get();
        System.out.println(documentFields);
        System.out.println(documentFields.getSource());
    }

    @Test
    public void TestComplex(){
        TransportClient client = ESClientUtil.getClient();
        SearchRequestBuilder Builder = client.prepareSearch("pet").setTypes("employee");
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.matchQuery("name", "李强"));
        boolQueryBuilder.filter(QueryBuilders.termQuery("sex",true))
                .filter(QueryBuilders.rangeQuery("age").gte(1).lte(12));
        Builder.setQuery(boolQueryBuilder);
        Builder.setFrom(0);
        Builder.setSize(5);
        SearchResponse age = Builder.addSort("age", SortOrder.DESC).get();
        SearchHit[] hits = age.getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }


    }
}
